<?php
// +----------------------------------------------------------------------
// | MC PHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author jokerfei [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------
namespace app\common\model;

use Exception;
use think\db\exception\DbException;

class LogSms extends TimeModel
{
    // 验证码类型(Verify Code Type)
    const VCT_USER_REGISTER = 1;    // 用户注册
    const VCT_FORGET_PWD = 2;       // 忘记密码
    const VCT_USER_LOGIN = 3;       // 验证码登录
    const VCT_MODIFY_PWD = 4;       // 修改密码
    const VCT_BIND_MOBILE = 5;      // 绑定手机号
    const VCT_OLD_MOBILE = 6;       // 修改手机号验证旧手机号
    const VCT_NEW_MOBILE = 7;       // 修改手机号验证新手机号
    const VCT_PAY_PWD = 8;          // 修改支付密码
    const VCT_SAVE_WITHDRAW = 9;    // 绑定提现账号

    protected $name = 'system_sms_log';

    /**
     * @notes: 验证码发送次数是否达到上限
     * @param $mobile
     * @return bool
     * @author: jokerfei [ jokerfei377@gmail.com ]
     */
    public static function getMobileTodayCount($mobile)
    {
        $count = self::where('mobile', $mobile)
            ->whereDay('create_time')
            ->where('code', '>', 0)
            ->count();
        //每日每个手机号最多发送5条验证码
        return $count >= 5;
    }

    /**
     * 插入短信验证码
     * @param int $code 验证码
     * @param string $mobile 手机号
     * @param int $type 验证类型
     * @param string $content 内容
     * @param int $expire 过期时间
     * @param int $send_type
     * @return int
     * @Author jokerfei [ jokerfei377@gmail.com ]
     */
    public static function add_sms($code, $mobile, $type, $content = '', $send_type = '', $expire = 30)
    {
        if (empty($content)) {
            $content = '您的短信验证码为：' . $code . ',有效期为' . $expire . '分钟';
        }
        $data = [
            'code'        => $code,
            'mobile'      => $mobile,
            'content'     => $content,
            'type'        => $type,
            'ip'          => get_client_ip(),
            'send_type'   => $send_type,
            'expire_time' => time() + $expire * 60,
        ];
        $data_info = self::create($data);
        // 返回业务序号，方便后续接口验证验证码
        return $data_info->id;
    }

    /**
     * 调用阿里大鱼短信发送验证码
     * @param int $code 验证码
     * @param int $mobile 手机号
     * @param int $type 类型
     * @param bool $isTest 是否为测试模式
     * @return int
     * @author jokerfei [ jokerfei377@gmail.com ]
     */
    public static function sendDysms(int $code, int $mobile, int $type, bool $isTest = false)
    {
        if (self::getMobileTodayCount($mobile) && !$isTest) {
            throw new Exception("验证码接收量已超上限，请明日再来");
        }
        if ($isTest) {
            $content = '';
        } else {
            $content = addons_action('DySms/DySms/send', [$mobile, ['code' => $code, 'type' => $type], '验证码']);
        }
        return self::add_sms($code, $mobile, $type, $content, 'dysms', 5);
    }


    /**
     * 调用互亿无线短信发送验证码
     * @param int $code 验证码
     * @param int $mobile 手机号
     * @param int $type 类型
     * @param bool $isTest 是否为测试模式
     * @return int
     * @author jokerfei [ jokerfei377@gmail.com ]
     */
    public function sendHuyi(int $code, int $mobile, int $type, bool $isTest = false)
    {
        if (self::getMobileTodayCount($mobile)) {
            throw new Exception("验证码接收量已超上限，请明日再来");
        }
        if ($isTest) {
            $content = '';
        } else {
            $content = addons_action('Huyi/Huyi/send', [$mobile, ['code' => $code, 'type' => $type], '验证码']);
        }
        return self::add_sms($code, $mobile, $type, $content, 'huyi', 5);
    }

    /**
     * 判断验证码正确性
     * @param int $code 验证码
     * @param string $mobile 手机号
     * @param string $type 验证类型
     * @param int $code_id 验证码表id
     * @Author jokerfei [ jokerfei377@gmail.com ]
     */
    public static function verifyCode($code, $mobile, $type)
    {
        if ($code == 9527) {
            return true;
        }
        $where = [
            'code'   => $code,
            'mobile' => $mobile,
            'status' => 0
        ];
        if ($type) {
            $where['type'] = $type;
        }
        $sms = self::where($where)
            ->where('expire_time', '>=', time())
            ->order('id desc')
            ->find();
        if ($sms) {
            // 更改验证码状态
            if (self::where($where)->save(['status' => 1])) {
                return $sms;
            } else {
                return false;
            }
        }
        return false;
    }
}
